package com.example.qinghe.frontController.sale;

import com.example.qinghe.entity.Common;
import com.example.qinghe.entity.Page;
import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.entity.sale.Customerqh;
import com.example.qinghe.service.communication.StaffService;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.service.sale.CustomerqhService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/front/customerqh")
@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
@Api(tags = "销售管理->客户管理", description = "FrontCustomerqhController")
public class FrontCustomerqhController {

    @Autowired
    private CustomerqhService customerqhService;
    @Autowired
    private StaffService staffService;

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    //    @PathVariable 注解用于将 URL 路径中的参数绑定到方法的参数上，而 @RequestParam 注解用于将请求参数绑定到方法的参数上。表单提交
    @ApiOperation("所有客户")
    @GetMapping(value = "/selectCustomerList/{page}/{limit}")
    public Result selectCustomerList(@ApiParam(name = "page", value = "页数") @PathVariable(value = "page") int page,
                                     @ApiParam(name = "limit", value = "页条数") @PathVariable(value = "limit") int limit,
                                     @ApiParam(name = "company", value = "公司名称") @RequestParam(required = false) String company,
                                     @ApiParam(name = "name", value = "客户姓名") @RequestParam(required = false) String name,
                                     @ApiParam(name = "phone", value = "客户手机号") @RequestParam(required = false) String phone,
                                     @ApiParam(name = "field", value = "客户行业") @RequestParam(required = false) String field
    ) {

        Page page1 = new Page(page, limit);
        int count = customerqhService.countTotall(company, name, phone, field);
        List<Customerqh> customers = customerqhService.selectCustomerList(page1, page, limit, company, name, phone, field);
        if (count == 0 || customers == null)
            return Result.success(page1, "没有数据");
        System.out.println("fdshjkalfsda" + customers);
        page1.setCount(count);
        page1.setData(customers);
        return Result.success(page1);
    }


    @ApiOperation("根据客户id查找具体的客户,里面的stafflist是联系人取name和enterprisename就行了")
    @GetMapping(value = "/selectCustomerById/{id}")
    public Result selectCustomerById(@PathVariable("id") Long id) {
        Customerqh customerqh = customerqhService.selectCustomerById(id);
        String staffList = customerqh.getContactpeople();
        String[] split = staffList.split(",");
        System.out.println("========================" + Arrays.toString(split));
        for (String s : split) {
            System.out.println(s);
            long l = Long.parseLong(s);
            Common sel = staffService.sel(l);
            System.out.println("===========================" + sel);
            customerqh.getStaffList().add(sel);
        }
        return customerqh == null ? Result.error("出现错误") : Result.success(customerqh);
    }


//    @ApiOperation("员工id查找员工的所有客户")
//    @GetMapping(value = "/selectCustomerByStaffId/{id}")
//    public Result selectCustomerByStaffId(Long id){
//        List<Customerqh> customerqh = customerqhService.selectCustomerByStaffId(id);
//        return customerqh==null?Result.success("没有数据"):Result.success(customerqh);
//    }

    @ApiOperation("查找当前员工的所有客户")
    @GetMapping(value = "/selectCustomerByStaffId")
    public Result selectCustomerByStaffId(@ApiParam(name = "company", value = "编号") @RequestParam(required = false) String company, @ApiParam(name = "name", value = "客户姓名") @RequestParam(required = false) String name, @ApiParam(name = "phone", value = "电话号码") @RequestParam(required = false) String phone, @ApiParam(name = "field", value = "领域") @RequestParam(required = false) String field, HttpServletRequest request) {
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");
        List<Customerqh> customerqh = customerqhService.selectCustomerByStaffId(staff.getId(), company, name, phone, field);
        return customerqh == null ? Result.success("没有数据") : Result.success(customerqh);
    }

}